<template>
  <div class="employees-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <PageTools>
          <template #left>
            <span>总共多少条</span>
          </template>
          <template #right>
            <el-button type="warning">导入excel</el-button>
            <el-button type="danger" @click="excelEport">导出excel</el-button>
            <el-button type="primary" @click="showDialog = true">
              新增excel
            </el-button>
          </template>
        </PageTools>
      </el-card>
      <el-card style="margin-top: 10px">
        <el-table :data="list" border>
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="员工照片">
            <template #default="{row}">
              <img :src="row.staffPhoto" alt="" style="width: 100%;">
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{ row }">
              {{ formOfEmployentList(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <el-table-column label="操作" width="280">
            <template #default="{ row }">
              <el-button
                type="text"
                size="small"
                @click="$router.push(`/employees/detail?id=${row.id}`)"
              >查看</el-button>
              <el-button
                type="text"
                size="small"
                @click="hAss(row.id)"
              >分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(row.id)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            :current-page="page"
            :page-sizes="[2, 5, 10, 20]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <el-dialog title="新增员工" :visible.sync="showDialog" @close="hClose">
        <empDialog ref="mydept" @success="hSuccess" />
      </el-dialog>
      <!-- 分配角色弹层 -->
      <el-dialog :visible.sync="assginShowDialog" title="分配角色">
        <assginLoad v-if="assginShowDialog" :id="id" @success="assginShowDialog = false" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import assginLoad from './modules/assginLoad.vue'
import empDialog from './modules/empDialog.vue'
import { getEmployeeList, delEmployee } from '@/api/employess'
import employees from '@/constant/employees'
console.log(employees.hireType)
// const obj = {}
// employees.hireType.forEach((item) => {
//   obj[item.id] = item.value
// })
const obj = employees.hireType.reduce((acc, item) => {
  acc[item.id] = item.value
  return acc
}, {})
export default {
  components: {
    empDialog,
    assginLoad
  },
  data() {
    return {
      assginShowDialog: false,
      showDialog: false,
      list: [],
      page: 1,
      size: 5,
      total: 0,
      id: ''
    }
  },
  created() {
    this.loadEmployeeList()
  },
  methods: {
    hAss(id) {
      this.assginShowDialog = true
      this.id = id
    },
    excelEport() {
      const list = this.list
      const { tHeader, data } = this.formExcelList(list)
      console.log(tHeader, data, '123123')
      import('@/vendor/Export2Excel').then((excel) => {
        excel.export_json_to_excel({
          header: tHeader,
          data: data,
          filename: '',
          autoWidth: true,
          bookType: 'xlsx'
        })
      })
    },
    // 实现导出的格式化
    formExcelList(list) {
      const map = {
        id: '编号',
        password: '密码',
        mobile: '手机号',
        username: '姓名',
        timeOfEntry: '入职日期',
        formOfEmployment: '聘用形式',
        correctionTime: '转正日期',
        workNumber: '工号',
        departmentName: '部门',
        staffPhoto: '头像地址'
      }
      const one = list[0]
      console.log(one)
      if (!one) return
      const tHeader = Object.keys(one).map((en_key) => {
        return map[en_key]
      })
      console.log(tHeader)
      const data = list.map((item) => {
        console.log(item)
        console.log(Object.values(item))
        return Object.values(item)
      })
      console.log(data)
      return { tHeader, data }
    },
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          try {
            const res = await delEmployee(id)
            console.log(res)
            if (this.list.length === 1) {
              this.page--
              if (this.page <= 0) {
                this.page = 1
              }
            }
            this.$message.success(res.message)
            this.loadEmployeeList()
          } catch (error) {
            console.log(error)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    hClose() {
      this.$refs.mydept.reset()
    },
    hSuccess() {
      this.showDialog = false
      this.loadEmployeeList()
      this.total++
      const maxPage = Math.ceil(this.total / this.size)
      this.page = maxPage
    },
    formOfEmployentList(value) {
      return obj[value] || '未知'
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.page = 1
      this.size = val
      this.loadEmployeeList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.loadEmployeeList()
    },
    async loadEmployeeList() {
      try {
        const res = await getEmployeeList(this.page, this.size)
        console.log(res)
        this.$message.success(res.message)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
